package com.mdlib.helper.module.send.profile.fragments;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.TextView;

import com.ljlib.help.R;
import com.mdlib.helper.api.DataManager;
import com.mdlib.helper.api.network.RxHelper;
import com.mdlib.helper.api.network.RxSubscribe;
import com.mdlib.helper.base.BaseTitleFragment;
import com.mdlib.helper.event.ShareEvent;
import com.mdlib.helper.model.BaseResult;
import com.mdlib.helper.model.entity.ShareEntity;
import com.mdlib.helper.ui.UIHelper;
import com.mdlib.helper.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 作者：HQY on 16/11/15 17:20
 * 邮箱：hqy_xz@126.com
 */

public class SendShareFragment extends BaseTitleFragment {
    @Bind(R.id.tv_share_win)
    TextView mTvShareWin;
    @Bind(R.id.tv_share_tips)
    TextView mTvShareTips;
    @Bind(R.id.tv_share_money)
    TextView mTvShareMoney;
    private ShareEntity shareInfo;

    public static SendShareFragment newInstance() {
        return new SendShareFragment();
    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.fragment_send_share;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected void initView(View rootView) {
        super.initView(rootView);
        setTitle(R.string.me_tv_share, R.color.generic_color_black)
                .setTitleBgColor(R.color.generic_color_white)
                .setImgLeftBg(R.drawable.iv_share_title_back)
                .setImgLeftListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        removeFragment();
                    }
                });
        getShareInfo();
    }

    private void getShareInfo() {
        startLoadingDialog(true);
        addSubscribe(DataManager.getInstance().getUserApi()
                .getShareInfo()
                .compose(RxHelper.<ShareEntity>defaultHandleResult())
                .subscribe(new RxSubscribe<ShareEntity>() {
                    @Override
                    protected void onSucc(ShareEntity shareEntity) {
                        stopLoadingDialog();
                        shareInfo = shareEntity;
                        updateShare(shareInfo);
                    }

                    @Override
                    protected void onFail(String msg) {
                        stopLoadingDialog();
                        ToastUtil.showToast(msg);
                    }
                }));
    }

    private void updateShare(ShareEntity share) {
        String money = share.getCoupon_data().getValue();
        String str = money.substring(0, money.indexOf("."));
        mTvShareTips.setText(getActivity().getResources().getString(R.string.share_tv_tips, str));
        mTvShareMoney.setText(str);

    }

    @OnClick(R.id.tv_share_win)
    public void onClick() {

        UIHelper.showDialogShare(getActivity(), shareInfo);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(ShareEvent shareEvent) {
        if (shareEvent.getType() == 0) {
            shareSuccessSend();
        }

    }

    private void shareSuccessSend() {
        addSubscribe(DataManager.getInstance().getUserApi()
                .shareSuccess()
                .compose(RxHelper.<BaseResult>rxSchedulerHelper())
                .subscribe(new RxSubscribe<BaseResult>() {
                    @Override
                    protected void onSucc(BaseResult baseResult) {
                        ToastUtil.showToast(baseResult.msg);
                    }

                    @Override
                    protected void onFail(String msg) {
                        ToastUtil.showToast(msg);
                    }
                }));
    }

}
